package com.xm.servlet.admin;

import com.xm.biz.CartBiz;
import com.xm.biz.OrderBiz;
import com.xm.biz.OrderGoodsBiz;
import com.xm.biz.UserAddressBiz;
import com.xm.biz.impl.CartBizImpl;
import com.xm.biz.impl.OrderBizImpl;
import com.xm.biz.impl.OrderGoodsBizImpl;
import com.xm.biz.impl.UserAddressBizImpl;
import com.xm.dao.OrderSettlementsDao;
import com.xm.dao.impl.OrderSettlementsDaoImpl;
import com.xm.po.*;
import com.xm.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-08-26 12:13
 * @desc 订单管理处理器
 **/
@WebServlet("/cart")
public class CartServlet extends HttpServlet {
    //初始化数据源
    OrderSettlementsDao orderSettlementsDao = new OrderSettlementsDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        //删除购物车信息
        if (type.equals("cartDelete")){
            cartDelete(req,resp);
        }
        //新增商品到购物车
        else if (type.equals("cartInsert")){
            cartInsert(req,resp);
        }
        //查询购物车信息(修改)
        else if (type.equals("carSelect")){
            carSelect(req,resp);
        }
        //查询所有购物车信息
        else if (type.equals("queryCart")){
            queryCart(req,resp);
        }
        //修改购物车信息
        else if (type.equals("updateCart")){
            updateCart(req,resp);
        }
        //删除订单商品信息(后台)
        else if (type.equals("orderDelete")){
            orderDelete(req,resp);
        }
        //订单商品修改信息(查询)
        else if (type.equals("orderSelect")){
            orderSelect(req,resp);
        }
        //查询所有订单商品信息
        else if (type.equals("orderGoods")){
            orderGoods(req,resp);
        }
        //修改订单商品数据
        else if (type.equals("orderUpdate")){
            orderUpdate(req,resp);
        }
        //查询所有订单信息
        else if (type.equals("order")){
            order(req,resp);
        }
        //订单结算信息
        else if (type.equals("orderSettlement")){
            orderSettlement(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    //删除购物车信息
    public void cartDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer cartId = Integer.valueOf(req.getParameter("cartId"));
        CartBiz cartBiz=new CartBizImpl();
        boolean result = cartBiz.deleteCart(cartId);
        if (result){
            resp.sendRedirect("Carts");
        }else {
            PrintWriter out=resp.getWriter();
            out.print("<script>alert('删除失败！');location.href='Carts';</script>");
            out.flush();
            out.close();
        }
    }
    //新增商品到购物车
    public void cartInsert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        Integer userId=Integer.valueOf(req.getParameter("userId"));
        Integer goodsId=Integer.valueOf(req.getParameter("goodsId"));
        Integer goodsCnt=Integer.valueOf(req.getParameter("goodsCnt"));

        Cart cart=new Cart();
        cart.setUserId(userId);
        cart.setIsCheck(1);
        cart.setGoodsId(goodsId);
        cart.setGoodsCnt(goodsCnt);

        CartBiz cartBiz =new CartBizImpl();
        boolean result = cartBiz.add(cart);
        if (result){
            resp.sendRedirect("Carts");
        }else {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('新增失败');location.href='Carts';</script>");
            out.flush();
            out.close();
        }
    }
    //查询购物车信息(修改)
    public void carSelect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer cartId = Integer.valueOf(req.getParameter("cartId"));
        CartBiz cartBiz=new CartBizImpl();
        Cart cart = cartBiz.findInfo(cartId);
        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(cart));
        out.flush();
        out.close();
    }
    //查询所有购物车信息
    public void queryCart(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        CartBiz cartBiz=new CartBizImpl();
        List<Cart> list = cartBiz.getAll();
        req.setAttribute("cart",list);
        req.getRequestDispatcher("admin/cart.jsp").forward(req,resp);
    }
    //修改购物车信息
    public void updateCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer cartId =Integer.valueOf(req.getParameter("cartId"));
        Integer userId =Integer.valueOf(req.getParameter("userId"));
        Integer GoodsId=Integer.valueOf(req.getParameter("goodsId"));
        Integer GoodsCnt =Integer.valueOf(req.getParameter("goodsCnt"));

        Cart cart=new Cart();
        cart.setCartId(cartId);
        cart.setUserId(userId);
        cart.setGoodsId(GoodsId);
        cart.setGoodsCnt(GoodsCnt);

        CartBiz cartBiz = new CartBizImpl();
        boolean result=cartBiz.updateCart(cart);
        if(result){
            resp.sendRedirect("Carts");
        }else {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('修改失败！');location.href='Carts';</script>");
            out.flush();
            out.close();
        }
    }
    //删除订单商品信息(后台)
    public void orderDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        Integer id=Integer.valueOf(req.getParameter("id"));
        OrderGoodsBiz orderGoodsBiz=new OrderGoodsBizImpl();
        boolean result=orderGoodsBiz.orderDelete(id);
        if (result){
            resp.sendRedirect("orderGoods");
        }else {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('删除失败！');location.href='orderGoods';</script>");
            out.flush();
            out.close();
        }
    }
    //订单商品修改信息(查询)
    public void orderSelect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        OrderGoodsBiz orderGoodsBiz=new OrderGoodsBizImpl();
        OrderGoods orderGoods = orderGoodsBiz.getById(id);
        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(orderGoods));
        out.flush();
        out.close();
    }
    //查询所有订单商品信息
    public void orderGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        OrderGoodsBiz orderGoodsBiz=new OrderGoodsBizImpl();
        List<OrderGoods>list=orderGoodsBiz.getAll();
        req.setAttribute("OrderGoods",list);
        req.getRequestDispatcher("admin/orderGoods.jsp").forward(req,resp);
    }
    //修改订单商品数据
    public void orderUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        Integer id = Integer.valueOf(req.getParameter("id"));
        Integer orderId=Integer.valueOf(req.getParameter("orderId"));
        Integer goodsId=Integer.valueOf(req.getParameter("goodsId"));
        Integer goodsNums=Integer.valueOf(req.getParameter("goodsNums"));
        Double goodsPrice=Double.valueOf(req.getParameter("goodsPrice"));
        String goodsName=req.getParameter("goodsName");

        OrderGoods orderGoods=new OrderGoods();
        orderGoods.setId(id);
        orderGoods.setOrderId(orderId);
        orderGoods.setGoodsId(goodsId);
        orderGoods.setGoodsNums(goodsNums);
        orderGoods.setGoodsPrice(goodsPrice);
        orderGoods.setGoodsName(goodsName);

        OrderGoodsBiz orderGoodsBiz=new OrderGoodsBizImpl();
        boolean result=orderGoodsBiz.orderUpdate(orderGoods);
        if (result){
            resp.sendRedirect("cart?type=orderGoods");
        }
        else {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('修改失败');location.href='cart?type=orderGoods'</script>");
            out.flush();
            out.close();
        }
    }
    //查询所有订单信息
    public void order(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        OrderBiz orderBiz=new OrderBizImpl();
        UserAddressBiz userAddressBiz=new UserAddressBizImpl();
        //用户地址信息
        List<UserAddress>users=userAddressBiz.list();
        //订单信息
        List<Order> list = orderBiz.gatAll();
        req.setAttribute("users",users);
        req.setAttribute("list",list);
        req.getRequestDispatcher("admin/order.jsp").forward(req,resp);
    }
    //订单结算信息
    public void orderSettlement(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<OrderSettlements> all = orderSettlementsDao.getAll();
        req.setAttribute("list",all);
        req.getRequestDispatcher("admin/orderSettlements.jsp").forward(req,resp);
    }
}
